package com.chenfan.ccp.plug.business.parser;

import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 解析模版生成需要被存储的数据
 *
 * @author xutong
 */
public class TemplateLogExpressionEvaluator extends CachedExpressionEvaluator {
    private Map<ExpressionKey, Expression> expressionCache = new ConcurrentHashMap<>(64);

    private final Map<AnnotatedElementKey, Method> targetMethodCache = new ConcurrentHashMap<>(64);

    public Object parseExpression(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
        return getExpression(this.expressionCache, methodKey, conditionExpression).getValue(evalContext, Object.class);
    }

    public EvaluationContext createEvaluationContext(Object rootObject, Method method, Object[] arguments,
                                                     Object ret, String errorMsg) {
        LogRecordEvaluationContext context = new LogRecordEvaluationContext(rootObject, method, arguments, getParameterNameDiscoverer(), ret, errorMsg);
        return context;
    }
}
